﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using WorkLogSystem.Entity;
using WorkLogSystem.Entity.Model;
using WorkLogSystem.Service;

namespace WorkLogSystem.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class CustomerDetailController : ControllerBase
    {
        private readonly BaseService<CustomerDetail> _service;
        public CustomerDetailController(BaseService<CustomerDetail> service)
        {
            _service = service;
        }

        [HttpPost]
        public ResultModel GetList(object data)
        {
            ResultModel result = new ResultModel();
            PageRequest request = JsonConvert.DeserializeObject<PageRequest>(data.ToString());
            result.Result = _service.GetList(request);
            result.Code = ResultCode.Success;
            result.Message = "获取成功";
            return result;
        }

        [HttpPost]
        public ResultModel GetPageList(PageRequest request)
        {
            ResultModel result = new ResultModel();
            result.Result = _service.GetPageList(request);
            result.Code = ResultCode.Success;
            result.Message = "获取成功";
            return result;
        }

        [HttpPost]
        public ResultModel AddItem(object data)
        {
            CustomerDetail customer = JsonConvert.DeserializeObject<CustomerDetail>(data.ToString());
            ResultModel result = new ResultModel();
            result.Result = _service.AddItem(customer);
            result.Code = ResultCode.Success;
            result.Message = "添加成功";
            return result;
        }

        [HttpPost]
        public ResultModel UpdateItem(object data)
        {
            ResultModel result = new ResultModel();
            Dictionary<string, object> customer = JsonConvert.DeserializeObject<Dictionary<string, object>>(data.ToString());
            if (string.IsNullOrWhiteSpace(customer["ID"].ToString()))
            {
                result.Code = ResultCode.Fail;
                result.Message = "ID为空，修改失败";
                return result;
            }
            result.Result = _service.UpdateItem(customer);
            result.Code = ResultCode.Success;
            result.Message = "修改成功";
            return result;
        }

        [HttpPost]
        public ResultModel DeleteItem(object data)
        {
            ResultModel result = new ResultModel();
            Dictionary<string, object> customer = JsonConvert.DeserializeObject<Dictionary<string, object>>(data.ToString());
            if (string.IsNullOrWhiteSpace(customer["ID"].ToString()))
            {
                result.Code = ResultCode.Fail;
                result.Message = "ID为空，删除失败";
                return result;
            }
            result.Result = _service.DeleteItem(customer);
            result.Code = ResultCode.Success;
            result.Message = "删除成功";
            return result;
        }
    }
}
